<template>
 <div>
   <div class="formula-editor">
     <textarea ref="textareaRef"></textarea>
   </div>
   <button @click="insert('apple')">add</button>
   text: {{text}}
 </div>
</template>

<script>
import "codemirror";
import "codemirror/addon/mode/overlay.js";
import "codemirror/addon/hint/show-hint.js";
import "codemirror/addon/hint/show-hint.css";
import "codemirror/lib/codemirror.css";
import "codemirror/theme/material-darker.css";
import "codemirror/addon/edit/matchbrackets";
import CodeMirror from "codemirror";
import "../mode";
import {thenable} from '@triascloud/utils';

export default {
  name: "DemoItem",
  data: () => ({
    $cm: null,
    text: '',
  }),
  mounted() {
    this.init();
  },
  methods: {
    init() {
      const keywords = [
        "{{vue}}",
        "{{react}}",
        "{{vite}}",
        "{{java}}",
        "{{vue-cli}}",
        "{{redux}}",
        "{{vue-router}}",
        "{{vuex}}",
      ];
      this.$cm = CodeMirror.fromTextArea(this.$refs.textareaRef, {
        mode: "variable",
        matchBrackets: {
          maxScanLines: 2000,
          maxHighlightLineLength: 200,
        },
        lineWrapping: true,
        keywords: keywords,
      });
      this.$cm.on("change", () => {
        this.$cm.showHint({
          completeSingle: false,
        });
       
      });
      this.$cm.on("hint-select", (fnName) => {
        console.log('aa', fnName);
        this.text = fnName.text.slice(2, -2);
        fnName.hint = () => {
          this.insert(fnName.text);
        };
      });
      // this.$cm.on("inputRead", () => {
      //   this.$cm.showHint();
      // });
    },
    insert(s) {
      this.insertVariable(s);
    },
    insertVariable(variable) {
      const cursor = this.$cm.getCursor();
      this.$cm.replaceSelection(`${variable}`);
      this.markVariable({
        start: cursor,
        end: this.$cm.getCursor(),
        variable,
      });
      this.$cm.setCursor(this.$cm.getCursor());
      this.$cm.focus();
    },
    markVariable(mark) {
      const fieldDom = document.createElement("span");
      /** @type { string | Promise<string> } */
      const label = mark.variable;
      thenable(label).then((label) => {
        fieldDom.title = label;
        fieldDom.innerText = label;
        fieldDom.classList.add(
          "formula-fields--item-type",
          "type-text",
          "formula-editor--field"
        );
      });
      fieldDom.dataset.variable = mark.variable;
      this.$cm.markText(mark.start, mark.end, {
        replacedWith: fieldDom,
        // handleMouseEvents: true,
        atomic: true,
      });
    },
  },
};
</script>

<style lang="less">
@prefix: formula-editor;
.@{prefix} {
  display: block;
  padding: 0;
  cursor: text;
  height: 140px;
  border: 1px solid purple;
  border-radius: 4px;
  width: 500px;
  textarea {
    width: 100%;
    height: 100%;
    border: none;
    background-color: transparent;
    color: #2ad181;
    &:focus {
      outline: none;
    }
  }
  .CodeMirror {
    padding: 4px;
    background-color: transparent;
    height: 100%;
    color: #2ad181;
  }
  .CodeMirror pre.CodeMirror-line,
  .CodeMirror pre.CodeMirror-line-like {
    font-family: Consolas, "Microsoft YaHei",serif;
  }
  pre.CodeMirror-line,
  pre.CodeMirror-line-like {
    line-height: 24px;
    outline: none;
  }
  .CodeMirror-wrap {
    pre.CodeMirror-line,
    pre.CodeMirror-line-like {
      word-break: break-all;
    }
  }
  pre.CodeMirror-placeholder {
    color: #845deb;
  }
  .CodeMirror-cursor {
    border-color: #2ad181;
  }
  .CodeMirror-selected {
    background-color: #fa9b47;
  }
  div.CodeMirror span.CodeMirror-nonmatchingbracket {
    color: #f11b1b;
    position: relative;
    &::before {
      content: "";
      position: absolute;
      bottom: -6px;
      left: -20%;
      right: 20%;
      height: 6px;
      border-top: 1px solid #f11b1b;
      border-radius: 50%;
    }
    &:after {
      content: "";
      position: absolute;
      bottom: -1px;
      left: 60%;
      right: -60%;
      height: 6px;
      border-bottom: 1px solid #f11b1b;
      border-radius: 50%;
    }
  }
  div.CodeMirror span.CodeMirror-matchingbracket {
    color: inherit;
    background-color: rgba(132, 93, 235, 0.3);
  }
  .@{prefix}--field {
    display: inline-block;
    margin: 1px;
    font-family: 'Microsoft YaHei', Arial, sans-serif;
  }
  .formula-fields--item-type {
    font-size: 12px;
    padding: 0 6px;
    min-width: 60px;
    height: 22px;
    line-height: 20px;
    text-align: center;
    border-radius: 11px;
    color: #fff;
    border: 1px solid;
    max-width: 260px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    vertical-align: bottom;
    &.type-text {
      border-color: #fab30b;
      background-color: #fab30b;
    }
  }
  .dark .formula-fields--item-type {
    &.type-text {
      color: #fab30b;
      background-color: rgba(250, 179, 11, 0.15);
    }
  }
  .cm-negative {
    color: #845deb;
  }
}
.CodeMirror-hints {
  font-family: Consolas, "Microsoft YaHei",serif;
}
.CodeMirror-hints {
  z-index: 10000;
  font-size: 14px;
}
</style>